﻿using System;
using System.Configuration;
using System.Data;
using System.Collections;
using digit.Core;


namespace SMS.ServiceManager
{
    public class MessageInManager
    {
        #region CONSTRUCTORS
        static MessageInManager()
        {
            _data = new MessageInData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static MessageInData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(long requestID, string mobileNo, string serviceID, string commandCode, string msgContent, DateTime requestTime, int status)
        {
            _error = "";
            try
            {
                object[] values = new object[7];
                values[0] = requestID;
                values[1] = mobileNo;
                values[2] = serviceID;
                values[3] = commandCode;
                values[4] = msgContent;
                values[5] = requestTime;
                values[6] = status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(MessageInInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[7];
                values[0] = info.RequestID;
                values[1] = info.MobileNo;
                values[2] = info.ServiceID;
                values[3] = info.CommandCode;
                values[4] = info.MsgContent;
                values[5] = info.RequestTime;
                values[6] = info.Status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(long msgInID, long requestID, string mobileNo, string serviceID, string commandCode, string msgContent, DateTime requestTime, int status)
        {
            _error = "";
            try
            {
                object[] values = new object[8];
                values[0] = msgInID;
                values[1] = requestID;
                values[2] = mobileNo;
                values[3] = serviceID;
                values[4] = commandCode;
                values[5] = msgContent;
                values[6] = requestTime;
                values[7] = status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError80(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError80(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError81(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError81(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError82(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError82(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError83(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError83(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError84(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError84(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError85(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError85(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError86(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError86(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError87(long msgInID, string mobile, string content, byte status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, content, status };
                return _data.UpdateError87(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateWaitingService()
        {
            _error = "";
            try
            {
                return _data.UpdateWaitingService();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static DataTable SelectAll_MessageError_ByServiceID(string serviceId, string userName)
        {
            _error = "";
            try
            {
                object[] values = { serviceId, userName };

                return _data.SelectAll_MessageError_ByServiceID(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll_Network_ByServiceID(string serviceId)
        {
            _error = "";
            try
            {
                return _data.SelectAll_Network_ByServiceID(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool UpdateReturnProcess(long MsgInID, string serviceId, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[3];
                values[0] = MsgInID;
                values[1] = serviceId;
                values[2] = status;

                return _data.UpdateReturnProcess(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(MessageInInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[8];
                values[0] = info.MsgInID;
                values[1] = info.RequestID;
                values[2] = info.MobileNo;
                values[3] = info.ServiceID;
                values[4] = info.CommandCode;
                values[5] = info.MsgContent;
                values[6] = info.RequestTime;
                values[7] = info.Status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(long msgInID, string mobile, string serviceid)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, serviceid };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(MessageInInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.MsgInID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static MessageInInfo Select(long msgInID)
        {
            _error = "";
            try
            {
                object[] values = { msgInID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                MessageInInfo info = new MessageInInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectByServiceID(string serviceId)
        {
            _error = "";
            try
            {
                return _data.SelectByServiceID(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgIn_ByServiceID_Online(string serviceId)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgIn_ByServiceID_Online(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SearchMsgIn_ByServiceID_Online(string serviceId, string content, DateTime fromDate, DateTime toDate, int typ)
        {
            _error = "";
            try
            {
                object[] values = { serviceId, content, fromDate, toDate, typ };
                return _data.SearchMsgIn_ByServiceID_Online(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgIn_ByService_Online(string service)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgIn_ByService_Online(service);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgIn_ByServiceID(string serviceId)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgIn_ByServiceID(serviceId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgIn_ByService(string service)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgIn_ByService(service);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgIn_ByBet(string service, string msgType)
        {
            _error = "";
            try
            {
                object[] values = { service, msgType };

                return _data.StatisticMsgIn_ByBet(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable StatisticMsgInError_ByEdit(string service)
        {
            _error = "";
            try
            {
                return _data.StatisticMsgInError_ByEdit(service);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable GetSystemVars(string serId)
        {
            _error = "";
            try
            {
                return _data.GetSystemVars(serId);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable GetSystemVars996()
        {
            _error = "";
            try
            {
                return _data.GetSystemVars996();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll_MessageError()
        {
            _error = "";
            try
            {
                return _data.SelectAll_MessageError();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static ArrayList SelectAll_MessageProcess()
        {
            _error = "";
            try
            {
                DataTable dt = _data.SelectAll_MessageProcess();

                if (dt.Rows.Count < 0) return null;

                ArrayList arrMsgin = new ArrayList();

                foreach (DataRow dr in dt.Rows)
                {
                    MessageInInfo msgIn = new MessageInInfo();

                    msgIn.MsgInID = long.Parse(dr["MsgInID"].ToString());
                    msgIn.RequestID = int.Parse(dr["RequestID"].ToString());
                    msgIn.RequestTime = Convert.ToDateTime(dr["RequestTime"].ToString());
                    msgIn.MobileNo = dr["MobileNo"].ToString();
                    msgIn.ServiceID = dr["ServiceID"].ToString();
                    msgIn.MsgContent = dr["MsgContent"].ToString();
                    msgIn.CommandCode = dr["CommandCode"].ToString();
                    msgIn.Status = byte.Parse(dr["Status"].ToString());

                    arrMsgin.Add(msgIn);
                }
                return arrMsgin;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool UpdateStatus(long msgInID, int status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, status };
                return _data.UpdateStatus(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool UpdateError(long msgInID, string content, int status)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, content, status };
                return _data.UpdateError(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool MoveError(long msgInID, string mobile, string service, string username)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, service, username };
                return _data.MoveError(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool DeleteError(long msgInID, string mobile, string service, string username)
        {
            _error = "";
            try
            {
                object[] values = { msgInID, mobile, service, username };
                return _data.DeleteError(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool InsertError(string RequestID, DateTime ResponseTime, string Error, string Type)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = RequestID;
                values[1] = ResponseTime;
                values[2] = Error;
                values[3] = Type;

                return _data.InsertError(Error);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        #endregion

    }
}